/*
 * $Id: SaveAction.java 24 2009-03-31 23:40:10Z aaron.day $
 */
package org.crazydays.gameplan.data;


import java.awt.event.ActionEvent;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.crazydays.gameplan.GamePlan;
import org.crazydays.gameplan.swing.AbstractActionWrapper;

/**
 * SaveAction
 */
public class SaveAction
        extends AbstractActionWrapper
{
	/** serial version uid */
	public final static long serialVersionUID =
	        "$Id: SaveAction.java 24 2009-03-31 23:40:10Z aaron.day $"
	                .hashCode();

	/** log */
	protected static Log LOG = LogFactory.getLog(SaveAction.class);

	/** game plan */
	protected GamePlan gamePlan;

	/**
	 * SaveAction constructor.
	 * 
	 * @param gamePlan GamePlan
	 */
	public SaveAction(GamePlan gamePlan)
	{
		super("Save", loadIcon("/com/famfamfam/silk/disk.png"));
		putValue(SHORT_DESCRIPTION, null);
		putValue(MNEMONIC_KEY, null);
		setGamePlan(gamePlan);
	}

	/**
	 * Set game plan.
	 * 
	 * @param gamePlan GamePlan
	 */
	public void setGamePlan(GamePlan gamePlan)
	{
		this.gamePlan = gamePlan;
	}

	/**
	 * Get game plan.
	 * 
	 * @return GamePlan
	 */
	public GamePlan getGamePlan()
	{
		return gamePlan;
	}

	/**
	 * Save current game plan database.
	 * 
	 * @param event Event
	 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
	 */
	public void actionPerformed(ActionEvent event)
	{
		Database database = getGamePlan().getDatabase();
		if (database.getFile() == null) {
			LOG.warn("GamePlan database file unset!");
		} else {
			try {
				database.save();
			} catch (IOException e) {
				LOG.error("Unable to save database", e);
			}
		}
	}
}
